package com.fengshang.wedding.domain.service.internal;

import java.util.Collection;

import org.springframework.transaction.annotation.Transactional;

import com.fengshang.wedding.domain.model.internal.TopicEntity;
import com.fengshang.wedding.domain.model.internal.UserEntity;

/**
 * 
 *@author gembin@gmail.com
 * 
 */
@Transactional
public class TopicPersistenceService extends AbstractPersistenceService<TopicEntity> implements ITopicPersistenceService {
	
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.ITopicPersistenceService#findUserTopicsByPagination(int, int, int)
	 */
	public Collection<TopicEntity> findUserTopicsByPagination(int userId,
			int pageSize, int currentPage) {
		return findByPagination(false,"SELECT t FROM "+entityName()+" t WHERE t.owner="+userId+" ORDER BY t.postDate DESC", pageSize, currentPage);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.ITopicPersistenceService#findTopicsByPagination(java.lang.String, int, int)
	 */
	public Collection<TopicEntity> findTopicsByPagination(String query,
			int pageSize, int currentPage) {
		return findByPagination(false,query, pageSize, currentPage);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.ITopicPersistenceService#findTopicById(int)
	 */
	public TopicEntity findTopicById(int topicId) {
		return findById(TopicEntity.class, topicId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.ITopicPersistenceService#updateTopic(com.fengshang.wedding.domain.model.internal.TopicEntity)
	 */
	public TopicEntity updateTopic(TopicEntity topic) {
		return update(topic);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.ITopicPersistenceService#removeTopic(int)
	 */
	public void removeTopic(int topicId) {
		remove(TopicEntity.class, topicId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.ITopicPersistenceService#createTopic(int, com.fengshang.wedding.domain.model.internal.TopicEntity)
	 */
	public void createTopic(int userId, TopicEntity topic)throws EntityNotFoundException {
		UserEntity user = getEntityManager().find(UserEntity.class, userId);
		System.out.println("user:" + user);
		if(user==null){
			throw new EntityNotFoundException("User with id="+userId+" dose not exists!!");
		}
		user.addTopic(topic);
		getEntityManager().merge(user);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.ITopicPersistenceService#getTotalPages(int)
	 */
	public int getTotalPages(int pageSize) {
		return getEntityTotalPages(pageSize);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.ITopicPersistenceService#getTotalPages()
	 */
	public int getTotalPages() {
		return getEntityTotalPages();
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.AbstractPersistenceService#entityName()
	 */
	public String entityName() {
		return TOPIC_ENTITY_NAME;
	}
}
